#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

        ui->setupUi(this);



       db=QSqlDatabase::addDatabase("QSQLITE");//����������� � ����
       db.setDatabaseName(":memory:");
       //��������� ����������
       db.open();


    QSqlQuery query(db);

        ui->lineEdit->setText("VVedi studenta");




    query.exec("create table information1(familia ,imia varchar(20), otchestvo varchar(20),"
                        "facultet varchar(5)  , gruppa varchar(5) )");


  query.exec("create table information2(familia varchar(20),imia varchar(20),"
                        "nomer varchar(12))");



}

MainWindow::~MainWindow()
{delete ui;
    db.close();

}





void MainWindow::on_pushButton_3_clicked()
{
    tab();

}
void MainWindow::tab()
{


    QSqlRelationalTableModel *model3=new QSqlRelationalTableModel(0);
    model3->setTable("information1");
    model3->setSort(1, Qt::AscendingOrder);
    model3->select();

        view.setModel(model3);
        view.show();


}

void MainWindow::tab2()
{
    QSqlRelationalTableModel *model2=new QSqlRelationalTableModel(0);
    model2->setTable("information2");
    model2->setSort(1, Qt::AscendingOrder);
    model2->select();

        view.setModel(model2);
        view.show();


}

void MainWindow::on_pushButton_2_clicked()
{
    QString str=ui->lineEdit_2->text();
    QStringList list=str.split(",");
    if(list.size()==2)
    {
    QSqlQuery query(db);
    query.next();
    if(!query.exec("delete from information1 where familia='"+list[0]+"' and imia='"+list[1]+"' ;"))
        ; else (!query.exec("delete from information2 where familia='"+list[0]+"' and imia='"+list[1]+"' ;"));


    query.exec();
}

else
{
msgBox.setWindowTitle("oShipka");
msgBox.setText("(o_O)");
msgBox.setIcon(QMessageBox::Information);
msgBox.setInformativeText("Vse poteeriano programma delete");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
}
void MainWindow::on_pushButton_clicked()
{
    {QSqlQuery query(db);

        QString str=ui->lineEdit->text();
        QStringList list=str.split(",");
        QString *str2=new QString[5];

        if(list.size()==5)
        {
            for(int i=0;i<list.size();i++)
            {
                str2[i]=list[i];
            }

                    query.next();
                    query.prepare("insert into information1 (familia, imia, otchestvo, facultet, gruppa) "
                                          "values (:familia, :imia, :otchestvo, :facultet, :gruppa)");

                    query.bindValue(":familia",str2[0]);
                    query.bindValue(":imia",str2[1]);
                    query.bindValue(":otchestvo",str2[2]);
                           query.bindValue(":facultet",str2[3]);
                    query.bindValue(":gruppa",str2[4]);
                    query.exec();

        }
        else
        {
            msgBox.setWindowTitle("OSHIPKA");
                msgBox.setText("Text neveren");
                msgBox.setIcon(QMessageBox::Information);
                msgBox.setInformativeText("Poprobui 4erez zapiatyu vse! ");
                msgBox.setStandardButtons(QMessageBox::Ok);
                msgBox.setDefaultButton(QMessageBox::Ok);
                msgBox.exec();
        }
        delete []str2;
    }
}
void MainWindow::on_pushButton_4_clicked()
{
    db.close();
    close();
}

void MainWindow::on_pushButton_6_clicked()
{
    QSqlQuery query(db);

            QString str=ui->lineEdit_3->text();
            QStringList list=str.split(",");
            QString *str2=new QString[5];

            if(list.size()==3)
            {
                for(int i=0;i<list.size();i++)
                {
                    str2[i]=list[i];
                }

                        query.next();
                        query.prepare("insert into information2 (familia, imia, nomer) "
                                              "values (:familia, :imia, :nomer)");

                        query.bindValue(":familia",str2[0]);
                        query.bindValue(":imia",str2[1]);
                        query.bindValue(":nomer",str2[2]);

                        query.exec();

            }
            else
            {
                msgBox.setWindowTitle("OSHIPKA");
                    msgBox.setText("Text neveren");
                    msgBox.setIcon(QMessageBox::Information);
                    msgBox.setInformativeText("Poprobui 4erez zapiatyu vse! ");
                    msgBox.setStandardButtons(QMessageBox::Ok);
                    msgBox.setDefaultButton(QMessageBox::Ok);
                    msgBox.exec();
            }
            delete []str2;
        }


void MainWindow::on_pushButton_5_clicked()
{
    tab2();
}

